/* MAIN.C file
 * 
 * Copyright (c) 2002-2005 STMicroelectronics
 */


/* Includes ------------------------------------------------------------------*/
#include "common.h"
#include "init.h"
#include "bluetooth.h"
#include "rb112.h"
#include "led.h"
#include "timer_ms.h"
#include "button.h"
#include "bp_module.h"
#include "TEST.h"
#include "crc16.h"

/* Uncomment the line below to expanse the "assert_param" macro in the 
   Standard Peripheral Library drivers code */
/* #define USE_FULL_ASSERT    (1) */ 

/* Exported macro ------------------------------------------------------------*/


/** @addtogroup STM8L15x_StdPeriph_Examples
  * @{
  */

/**
  * @addtogroup USART_HyperTerminal_DMA
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/


/* Private macro -------------------------------------------------------------*/
#define countof(a)            (sizeof(a) / sizeof(*(a)))



/* Private function prototypes -----------------------------------------------*/


/* Private functions ---------------------------------------------------------*/

enum
{
	SYS_INIT,
	SYS_STANDBY,
	SYS_RUN,
	SYS_BLUETOOTH_UP,
	SYS_PREPARE_SLEEP,
	SYS_SLEEP,
	SYS_WAKEUP
};



/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
char data1[] = "1234567890";
char data2[] = "abcdefghij";

uint8_t cur_bt_state = BT_S_NONE;
void led_manager(void)
{
	uint8_t bt_state = bt_get_state();
	if(bt_state == cur_bt_state)
		return;

	cur_bt_state = bt_state;
	switch(cur_bt_state)
	{
	case BT_S_OFF:
		led_flash_stop();
		led_off();
		break;
	case BT_S_UNCONNECT:
		led_flash_start(LED_MEDIUM_FLASH);
		break;
	case BT_S_CONNECTED:
		led_flash_stop();
		led_on();
		break;
	}
}

void main(void)
{	
	uint16_t led_ms = 100;
	uint8_t press_state;
	uint8_t sys_state = SYS_INIT;
	uint8_t bt_state;
	uint8_t module_state;
	uint8_t i=0;
	char reply_str[8][16];
	uint8_t data[] = {0x0a, 0x02, 0x20, 0x50};
  uint16_t crc;
	char *reply;

   // __enable_interrupt();
	init();
	led_init();
	timer_ms_init();
	
   
	//crc16_buffer(data, 4, &crc);
	
#if 0
	while(1)
	{
		switch(sys_state)
		{
		case SYS_INIT:
			led_init();
			
			init_btn(USER_BUTTON);
			start_btn_monitor(USER_BUTTON);
			led_off();
			sys_state = SYS_STANDBY;
			break;
		case SYS_STANDBY:
			if(PRESS_1S == check_fetch_btn_event(USER_BUTTON))
			{
				/* power up bluetooth module */
				//TODO


				sys_state = SYS_RUN;
			}
			break;
		case SYS_RUN:
			//led_manager();
			//bt_manager();
			//module_manager();
			press_state = check_fetch_btn_event(USER_BUTTON);
			switch(press_state)
			{
			case PRESS_200MS:
				/* start send data */
				break;
			case PRESS_4S:
//				bt_disconnect();
				break;
			}
			break;

		}
	}
#endif
	
	 led_on();
	timer_ms_start_singleshot(GENERIC_TIMER, led_ms);
	while(1)
	{
		if(check_fetch_btn_event(USER_BUTTON) != NO_PRESS)
		{			
			if(led_ms == 100)
				led_ms = 500;
			else
				led_ms = 100;
		}
		if(timer_ms_test_and_clear_expired(GENERIC_TIMER))
		{
			//led_toggle();
			timer_ms_start_singleshot(GENERIC_TIMER, led_ms);
		}
	}
		
			
	bt_uart_init();
	//enableInterrupts();
	rb_bt_init();

	//if(rb_send_set_cmd(CMD_SET_SPP_DISCOVERABLE, "0", 1)==0)
	//	led_on();	
	//rb_send_get_cmd(CMD_GET_SPP_DISCOVERABLE, reply_str[0], 16);
	//if(rb_send_set_cmd(CMD_SET_SPP_DISCOVERABLE, "1", 1)==0)
	//	led_on();	
	rb_send_get_cmd(CMD_GET_SPP_DISCOVERABLE, reply_str[i++], 16);	
	
	//if(rb_send_set_cmd(CMD_SET_PIN, "2254", 4)==0)
		//led_on();
	rb_send_get_cmd(CMD_GET_VERSION, reply_str[i++], 16);
	rb_send_get_cmd(CMD_GET_NAME, reply_str[i++], 16);
	rb_send_get_cmd(CMD_GET_PIN, reply_str[i++], 16);
	rb_send_get_cmd(CMD_GET_COD, reply_str[i++], 16);
	rb_send_get_cmd(CMD_GET_BTADDR, reply_str[i++], 16);
	rb_send_get_cmd(CMD_GET_BAUD, reply_str[i++], 16);
	rb_send_get_cmd(CMD_GETIOCAP, reply_str[i++], 16);	
	
	led_on();
  while (1)
  {		
		
		
		
		//USART_SendData8(USART1, 0x33);
   // Delay((uint32_t)0xFFFFFF);
  }
	
}





/**
  * @}
  */
